/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package applet.Arguments;


import java.applet.*;
import java.net.*;
import library.Constants.*;
import library.Configuration.*;

import org.apache.log4j.*;

/**
 * This class is designed to access the arguments of the applet
 * The arguments of the applet are defined in the HTML file using the PARAM tag
 *  
 * @author JNC
 */
public class AppletArguments {
    /**
     * The log4j log
     */
    private static final Logger _logger = Logger.getLogger(AppletArguments.class);
    
    /**
     * This method finds the config filename in the parameters of the applet
     * The config filename is defined in the HTML file using the tag 
     * PARAM NAME="Config-Path"
     *
     * The config filename is prefixed with the codebase
     * The codebase is the root URL from which the config file can be downloaded
     *
     * @param myApplet
     * The running applet, which is required to get the codebase and the 
     * filename
     */
    public static void findConfigFilename(Applet myApplet) {
        URL codebase = null;
        String filename = null;

        // try to find the codebase
        // ------------------------
        codebase = getCodebase(myApplet);
        
        // try to find the filename
        // ------------------------
        filename = getFilename(myApplet);
        
        // builds the filename
        // -------------------
        if (codebase != null) {
            // run in applet mode, will use a URL to locate the config file
            try {
                Configuration.setConfigFilename(new URL(codebase, filename));
            }
            catch(java.net.MalformedURLException exception) {
                _logger.fatal("MalformedURLException caught (" + exception +")");
                _logger.info("Will use default config filename = " + Constants._DEFAULT_CONFIGFILENAME_VALUE);
                Configuration.setConfigFilename(Constants._DEFAULT_CONFIGFILENAME_VALUE);
            }
        }
        else {
            // run in application mode, will use a file to locate the config file
            // could happen if the applet is run via Main_Applet_Application
            Configuration.setConfigFilename(filename);
        }
    }
    
    /**
     * Gets the codebase, that is the root URL from which the config file can
     * be downloaded
     * 
     * @param myApplet
     * The running applet, which is required to get the codebase
     * @return
     * The URL that represents the codebase
     * In case of problem, returns null
     */
    private static URL getCodebase(Applet myApplet) {
        URL url = null;
        
        try {
            url = myApplet.getCodeBase();
        }        
        catch(java.lang.NullPointerException exception) {
            // getCodebase can throw NullPointerExceptoin if run before init()
            // could happen if the applet is run via Main_Applet_Application
            _logger.info("Caught NullPointerException (" + exception + ")");
            _logger.info("Could be normal if the applet is run directly through Netbeans by Project > Run");
        }
        _logger.info("codebase = " + url);
        return url;
    }
    
    /**
     * Gets the filename of the config file
     * 
     * @param myApplet
     * The running applet, which is required to get the parameters
     * @return
     * The filename of the config file
     * In case of problem, returns a default value (String)
     */
    
    private static String getFilename(Applet myApplet) {
        String filename = null;
        
        try {
            filename = myApplet.getParameter(Constants._CONFIGFILENAME_KEYWORD);
        }        
        catch(java.lang.NullPointerException exception) {
            // getCodebase can throw NullPointerExceptoin if run before init()
            // could happen if the applet is run via Main_Applet_Application
            _logger.info("Caught NullPointerException (" + exception + ")");
            _logger.info("Could be normal if the applet is run directly through Netbeans by Project > Run");
        }
        
        // If the parameter is not defined, or the NullPointerException is 
        // thrown
        if (filename == null) {
            _logger.info("Impossible to get the parameter " + Constants._CONFIGFILENAME_KEYWORD + " defined by the PARAM tag");
            _logger.info("Will use default value " + Constants._DEFAULT_CONFIGFILENAME_VALUE);
            filename = Constants._DEFAULT_CONFIGFILENAME_VALUE;
        }
        _logger.info("filename = " + filename);
        return filename;
    }
}